//
//  AXFShoppingController.swift
//  SpuerYangAngWang
//
//  Created by 杨茂盛 on 2016/12/22.
//  Copyright © 2016年 杨茂盛. All rights reserved.
//

import UIKit
let GoodsCellId = "GoodsCellId"
class AXFShoppingController: UIViewController,UITableViewDataSource {
    //控制器拥有一个管理购物车的
    //数据
    var dataSource: [ZHSellFastModel]?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setNav()
        self.setUI()
    }
}

extension AXFShoppingController{
    
    //MARK: 搭建界面
    func setUI(){
        self.title = "购物车"
        //判断否是有订单
        if self.dataSource?.count == 0 {
            //如果没有订单就显示默认的视图
            let norMalView = AXFShopNormalView.init(frame: UIScreen.main.bounds)
            norMalView.backgroundColor = #colorLiteral(red: 0.9119085727, green: 0.9119085727, blue: 0.9119085727, alpha: 1)
            self.view.addSubview(norMalView)
        } else{
        //否则显示有订单的视图
            let orderView = AXFShopOrderView.init(frame: UIScreen.main.bounds, style: .grouped)
            self.view.addSubview(orderView)
            orderView.dataSource = self
            let nibName = UINib.init(nibName: "AXFGoodsCellView", bundle: nil)
            orderView.register(nibName, forCellReuseIdentifier: GoodsCellId)
        }
    }
    
        //MARK: 设置导航栏
        func setNav() {
            let itemLeft = UIBarButtonItem.init(title: "", imageName: "v2_goback", titleFontSize: 12, target: self, action: #selector(scan))
            self.navigationItem.leftBarButtonItem = itemLeft
        }
        func scan(){
            self.navigationController!.dismiss(animated: true, completion: nil)
        }
        
    //MARK: 数据源方法
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource!.count 
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: GoodsCellId, for: indexPath) as! AXFGoodsCellView
      
        let model = self.dataSource![indexPath.row]
        cell.model = model
        return cell
    }
}
